﻿using Soft.Net;
using Soft.Service.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Soft.Service
{

    /// <summary>
    /// 连接信息
    /// </summary>
    internal sealed class ConnectedMappingInfo : SoftObject
    {


        public ConnectedMappingInfo(IPAddress iPAddress)
        {
            this.IPAddress = iPAddress;
            ResultData<RemoteServerMappingPerformer> remotingObject = NetManager.GetRemotingObject<RemoteServerMappingPerformer>((IP)iPAddress.ToString(), NetManager.MappingPort);
            if (remotingObject.Succeed)
            {
                this.Performer = remotingObject.Data;
                this.ResetTicks();
                return;
            }
            throw new Exception(remotingObject.Error.Text);
        }

 
        /// <summary>
        /// IP地址
        /// </summary>
        public IPAddress IPAddress { get; private set; }


        /// <summary>
        /// 服务端Mapping执行器
        /// </summary>
        public RemoteServerMappingPerformer Performer { get; private set; }


        /// <summary>
        /// 失败的次数
        /// </summary>
        public int FailedCount { get; set; }


        /// <summary>
        /// 是否断开连接
        /// </summary>
        public bool Disconnected
        {
            get
            {
                return DateTime.Now.Ticks - this.mLastTicks > 1000000L;
            }
        }


        /// <summary>
        /// 重置的时间点
        /// </summary>
        public void ResetTicks()
        {
            this.mLastTicks = DateTime.Now.Ticks;
        }

        
        /// <summary>
        /// 超时的时间点
        /// </summary>
        private const long OUT_TIME_TICKS = 1000000L;


        /// <summary>
        /// 最后访问的时间点
        /// </summary>
        private long mLastTicks;


    }
}
